﻿using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace com.fukefu
{
    public class MyProgressBar
    {
        private PictureBox pic;
        private Bitmap bt;
        private Graphics g;

        private int p_width = 0, p_height = 0, p_maxnum = 100, p_current_num = 0;
        private Pen p_border = new Pen(Color.FromArgb(170, 170, 170), 1);
        private SolidBrush p_background = new SolidBrush(Color.FromArgb(242, 242, 242));
        private SolidBrush p_font = new SolidBrush(Color.Black);
        private SolidBrush p_jindu = new SolidBrush(Color.FromArgb(127, 184, 127));
        private Font font = new Font("微软雅黑", 9, FontStyle.Bold);

        public MyProgressBar(PictureBox pic)
        {
            this.pic = pic;
            p_width = pic.Width - 1;
            p_height = pic.Height - 1;
            bt = new Bitmap(p_width, p_height);
            g = Graphics.FromImage(bt);
            FlushDate();
        }
        //设置最大数字
        public void SetMaxNum(int i)
        {
            p_maxnum = i;
            p_current_num = 0;
            FlushDate();
        }
        //增加一个数字
        public void Add()
        {
            p_current_num++;
            FlushDate();
        }
        //刷新数据
        private void FlushDate()
        {
            if (pic.InvokeRequired)
            {
                pic.BeginInvoke(new MyDelegate0(FlushDate));
            }
            else
            {
                if (p_current_num <= p_maxnum)
                {
                    int wd = p_maxnum != 0 ? p_current_num * (p_width - 3) / p_maxnum : 0;
                    //边框和背景
                    g.DrawRectangle(p_border, 0, 0, p_width, p_height);
                    g.FillRectangle(p_background, 1, 1, p_width, p_height);
                    //进度条和文字
                    if (wd != 0)
                    {
                        LinearGradientBrush b3 = new LinearGradientBrush(new Rectangle(2, 2, wd, p_height - 3), Color.PaleGreen, Color.LightSeaGreen, LinearGradientMode.Vertical);
                        g.FillRectangle(b3, 2, 2, wd, p_height - 3);
                    }
                    g.DrawString(p_current_num + "/" + p_maxnum, font, p_font, new Point(p_width - 80, 2));
                    //设置和刷新
                    pic.BackgroundImage = bt;
                    pic.Refresh();
                }
            }
        }
    }
}
